package com.efast.dmom.mes.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.dmom.mes.bean.QualityDefectGroup;
import com.efast.dmom.mes.bean.QualityGroup;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.datahandlers.entity.QualityDefectGroupHandler;
import com.efast.dmom.mes.datahandlers.entity.QualityGroupHandler;
import com.efast.dmom.mes.service.QualityGroupService;
@Service
public class QualityGroupServiceImpl implements QualityGroupService {

	@Autowired
	private QualityGroupHandler QualityGroupHandler = null;
	
	@Autowired 
	private QualityDefectGroupHandler qualityDefectGroupHandle = null;
	@Override
	public ResultBean save(QualityGroup qualityGroup) {
		ResultBean resultBean = new ResultBean(true);
		QualityGroup byCode = QualityGroupHandler.getByCode(qualityGroup.getInnerData().getGroupCode());
		if (byCode.getInnerData() != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("当前编码  "+qualityGroup.getInnerData().getGroupCode()+"  已存在！");
			return resultBean;
		}
		QualityGroupHandler.save(qualityGroup);
		return resultBean;
	}

	@Override
	public ResultBean batchDeleteById(List<QualityGroup> list) {
		ResultBean resultBean = new ResultBean(true);
		List<Long> groupList = new ArrayList<Long>();
		list.forEach(item -> {
			groupList.add(item.getInnerData().getId());
		});
		QualityGroupHandler.batchDeleteByIds(list);
		return resultBean;
	}

	@Override
	public ResultBean update(QualityGroup qualityGroup) {
		ResultBean resultBean = new ResultBean(true);
		QualityGroupHandler.update(qualityGroup);
		return resultBean;
	}

	@Override
	public ResultBean getById(long id) {
		ResultBean resultBean = new ResultBean(true);
		QualityGroupHandler.getById(id);
		return resultBean;
	}

	@Override
	public ResultBean selectByIds(List<QualityGroup> list) {
		ResultBean resultBean = new ResultBean(true);
		List<Long> groupIdList = new ArrayList<Long>();
		list.forEach(item -> {
			groupIdList.add(item.getInnerData().getId());
		});
		List<QualityDefectGroup> byGroupId = qualityDefectGroupHandle.getByGroupId(groupIdList);
		if (byGroupId.size()>0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("当前数据下存在不良代码,");
			return resultBean;
		}
		return resultBean;
	}

}
